package Utils;


import android.content.Context;
import android.content.SharedPreferences;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import net.ck.hzq.TobuyApplication;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import coom.RetrofitHelp;
import coom.TokenAuthenticator;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import util.StringUtils;

public class SystemDatas {
    // 测试
    private static final String URL_enbocompany = RetrofitHelp.BASE_URL + "api/";
//    private static final String URL_enbocompany = "http://47.99.215.106:8080/" + "api/";

    private static volatile SystemDatas sInstance;
    private Retrofit retrofit;
    private static Context mContext;

    private SystemDatas(Context context) {

        mContext = context.getApplicationContext();

        Gson gson = new GsonBuilder()
                .setLenient()
                .create();

        OkHttpClient client = ProgressManager.getInstance().with(new OkHttpClient.Builder()
                .readTimeout(120, TimeUnit.SECONDS)
                .writeTimeout(120, TimeUnit.SECONDS)
                .connectTimeout(120, TimeUnit.SECONDS))
                .authenticator(new TokenAuthenticator())
                .addInterceptor(new Interceptor() {
                            @Override
                            public Response intercept(Chain chain) throws IOException {
                                Request request = chain.request()
                                        .newBuilder()
                                        .addHeader("Authorization", StringUtils.isEmpty(TobuyApplication.token) ? "" : TobuyApplication.token)
                                        .build();
                                return chain.proceed(request);
                            }

                        })
                .build();

        retrofit = new Retrofit.Builder()
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
//                .baseUrl("http://www.baidu.com/")
                .baseUrl(URL_enbocompany)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build();

    }

    public static SystemDatas getInstance(Context context) {
        if (null == sInstance) {
            synchronized (SystemDatas.class) {
                if (null == sInstance) {
                    sInstance = new SystemDatas(context);
                }
            }
        }
        return sInstance;
    }

    public <T> T create(final Class<T> service) {
        return retrofit.create(service);
    }

    public static String GetService_URL(String type) {//, String token
        SharedPreferences sharedPreferences = mContext.getSharedPreferences("ToBuy", Context.MODE_PRIVATE);

//        String URLTOKEN_enbocompany = "?token=" + token;
        String url = "";
        if (type.equals("getorder")) {//获取不同状态订单号  未付款 未发货 已发货
            url = URL_enbocompany + "order/getOrderType";
        } else if (type.equals("getOrderDetails")) {//获取订单详情
            url = URL_enbocompany + "order/getOrderDetails";
        } else if (type.equals("login")) {//获取订单详情
            url = URL_enbocompany + "login";
        } else if (type.equals("cancelOrder")) {//取消订单
            url = URL_enbocompany + "order/cancelOrder";
        } else if (type.equals("cancelBank")) {//取消订单
            url = URL_enbocompany + "getMoney/delcar";
        } else if (type.equals("confimOrder")) {//确认订单
            url = URL_enbocompany + "order/confirmOrder";
        } else if (type.equals("getOrderDetails")) {//确认订单
            url = URL_enbocompany + "order/getOrderDetails";
        } else if (type.equals("getphonecode")) {//获取注册验证码
            url = URL_enbocompany + "getRegMsgCode";
        } else if (type.equals("register")) {//获取注册验证码
            url = URL_enbocompany + "register";
        } else if (type.equals("getforgetphonecode")) {//获取忘记密码验证码
            url = URL_enbocompany + "getForgetPasswordMsgCode";
        } else if (type.equals("modifypassword")) {//修改密码
            url = URL_enbocompany + "forgetPassdUpdatePassword";
        } else if (type.equals("getpickupmachine")) {
            url = URL_enbocompany + "posModel/getPosModelList";
        } else if (type.equals("getproduct")) {
            url = URL_enbocompany + "posModel/getPosModel";
        } else if (type.equals("getcity")) {
            url = URL_enbocompany + "area/test";
        } else if (type.equals("addaddress")) {
            url = URL_enbocompany + "address/addAddress";
        } else if (type.equals("getMyearnings")) {
            url = URL_enbocompany + "myEarnings/earnings";
        } else if (type.equals("history")) {
            url = URL_enbocompany + "myEarnings/history";
        } else if (type.equals("findAddress")) {
            url = URL_enbocompany + "address/findAddressBuCustomId";
        } else if (type.equals("deleteaddress")) {
            url = URL_enbocompany + "address/deleltAddress";
        } else if (type.equals("setdefaultaddress")) {
            url = URL_enbocompany + "address/defaultaddress";
        } else if (type.equals("getally")) {
            url = URL_enbocompany + "courseAlly/courseList";
        } else if (type.equals("complaint")) {
            url = URL_enbocompany + "complaint/addComplaint";
        } else if (type.equals("personal")) {
            url = URL_enbocompany + "custom/settlementIncome";
        } else if (type.equals("changepass")) {
            url = URL_enbocompany + "updatePassword";
        } else if (type.equals("getmessagecenter")) {
            url = URL_enbocompany + "notifyRecord/getMessageList";
        } else if (type.equals("getpos")) {
            url = URL_enbocompany + "merchant/getPos";
        } else if (type.equals("getphonecode2")) {//自助报单 手填 扫描 银行卡绑定 的验证码获取接口
            url = URL_enbocompany + "getAuthentication";
        } else if (type.equals("addOrder")) {
            url = URL_enbocompany + "order/addOrder";
        } else if (type.equals("alipay")) {
            url = URL_enbocompany + "pay/aliPay";
        } else if (type.equals("getachieve")) {
            url = URL_enbocompany + "custom/performance1";
        } else if (type.equals("getKnowledge")) {
            url = URL_enbocompany + "course/courseList";
        } else if (type.equals("getarea")) {
            url = URL_enbocompany + "activity/activityList";
        } else if (type.equals("getshare")) {
            url = URL_enbocompany + "custom/qrCode";
        } else if (type.equals("getbankcard")) {
            url = URL_enbocompany + "getMoney/car";
        } else if (type.equals("getreturn")) {
            url = URL_enbocompany + "custom/settlementIncome";
        } else if (type.equals("getforward")) {
            url = URL_enbocompany + "getMoney/applyMoney";
        } else if (type.equals("getpresentrecord")) {
            url = URL_enbocompany + "getMoney/record";
        } else if (type.equals("getAuthentication")) {
            url = URL_enbocompany + "getAuthentication";
        } else if (type.equals("addbankcard")) {
            url = URL_enbocompany + "getMoney/addcar";
        } else if (type.equals("getEnterprise")) {
            url = URL_enbocompany + "assistance/news";
        } else if (type.equals("updateapp")) {
            url = URL_enbocompany + "AppVersion/getLatestVersion";
        } else if (type.equals("getmainactivity")) {
            url = URL_enbocompany + "bannerinfo/list";
        } else if (type.equals("addmpos")) {
            url = URL_enbocompany + "merchant/addMPOS";
        } else if (type.equals("auth")) {
            url = URL_enbocompany + "submitIdCardInfo";
        } else if (type.equals("addpos1")) {
            url = URL_enbocompany + "merchant/addPOS1";
        }else if (type.equals("updateAddress")) {
            url = URL_enbocompany + "address/updateAddress";
        } else if (type.equals("personalIcon")) {
            url = URL_enbocompany + "uploadHeadImg";
        }
        return url;
    }

}